package inet.app.vod.adapter;

import inet.app.vod.activity.R;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MySpinnerAdapter extends ArrayAdapter<String> {

	private LayoutInflater mInflater;
	private String[] mObjects;
	private int mCurPosition;

	public MySpinnerAdapter(Context context, int textViewResourceId,
			String[] objects) {
		super(context, textViewResourceId, objects);
		this.mInflater = LayoutInflater.from(context);
		this.mObjects = objects;
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getCustomView(position, convertView, parent);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getCustomView(position, convertView, parent);
	}

	public View getCustomView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		final String content = mObjects[position];

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_spinner, parent,
					false);
			holder = new ViewHolder();
			holder.mContent = (TextView) convertView
					.findViewById(R.id.content_txt);
			holder.mSpinner = (ImageView) convertView
					.findViewById(R.id.spinner_img);
			holder.mMainLayout = (RelativeLayout) convertView
					.findViewById(R.id.main_layout);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mContent.setText(TextUtils.isEmpty(content) ? "" : content);
		if (position == mCurPosition) {
			holder.mSpinner.setImageResource(R.drawable.spin_select);
		} else {
			holder.mSpinner.setImageResource(R.drawable.spin_unselect);
		}

		return convertView;
	}

	public void update(int position) {
		this.mCurPosition = position;
		notifyDataSetChanged();
	}

	static class ViewHolder {
		public RelativeLayout mMainLayout;
		public TextView mContent;
		public ImageView mSpinner;
	}

}
